package ThreadOperation;

public class ThreadState
{
    public static void main(String[] args) throws InterruptedException
    {
        //创建线程
        Thread thread = new Thread(() ->
        {
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    Thread.sleep(100);
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            System.out.println("---------------------------");
        });
        //观察状态
        Thread.State state = thread.getState();
        System.out.println(state);
        //启动线程
        thread.start();
        state=thread.getState();
        System.out.println(state);
        //判断线程是否处于终止状态
        while (state != Thread.State.TERMINATED)
        {
            Thread.sleep(100);
            state = thread.getState();
            System.out.println(state);
        }
        //thread.start()不行，因为死亡后的线程不能重复启动
    }
}
